package com.lk.dao;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.util.List;

import com.lk.util.RowMapper;
import com.lk.entity.Idea;
import com.lk.util.DBHelp;

public class IdeaDao {
	
	public DBHelp<Idea> db = new DBHelp<Idea>();
	
	public Idea findById(String idea_id){
		String sql = "select id,i_name,i_describle,t_project_id,t_employee_id from t_idea where id = ?";
		return db.executeSQLForObject(sql.toString(),new IdeaRowMapper(),idea_id);
	}
	
	public void save(Idea i){
		String sql = "insert into t_idea(id,i_name,i_describle,t_project_id,t_employee_id) values(?,?,?,?,?)";
		db.executeSQL(sql,i.getI_id(),i.getI_name(),i.getI_describle(),i.getT_project_id(),i.getT_employee_id());
	}
	
	public void del(String id){
		String sql = "delete from t_idea where id = ?";
		db.executeSQL(sql, id);
	}
	
	public void update(Idea i){
		String sql = "update t_idea set i_name = ?,i_describle = ? where id = ?";
		db.executeSQL(sql, i.getI_name(),i.getI_describle(),i.getI_id());
	}
	
	public List<Idea> findAll(String t_project_id,String t_employee_id){
		String sql = "select id,i_name,i_describle,t_project_id,t_employee_id from t_idea where t_project_id = ? and t_employee_id = ?";
		return db.executeSQLForList(sql, new IdeaRowMapper(), t_project_id,t_employee_id);
	}
	
	public class IdeaRowMapper implements RowMapper<Idea>{
		public Idea  rowmapper(ResultSet rs) throws SQLException{
			Idea i = new Idea();
			i.setI_id(rs.getString("id"));
			i.setI_name(rs.getString("i_name"));
			i.setI_describle(rs.getString("i_describle"));
			
			i.setT_project_id(rs.getString("t_project_id"));
			i.setT_employee_id(rs.getString("t_employee_id"));
			return i;
		}
	}
}	
